package com.tywj.buscustomerapp.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.ApplicationLineBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.umeng.analytics.MobclickAgent;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class  ApplicationLineActivity extends BaseActivity {
    @BindView(R.id.head_left) ImageView back;
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.application_home) TextView home;
    @BindView(R.id.application_work) TextView work;
    @BindView(R.id.application_time) TextView time;
    @BindView(R.id.application_time1) TextView time1;
    @BindView(R.id.cs_submit) TextView submit;

    @Override
    protected int getContentId() {
        return R.layout.activity_application_line;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
    }

    @Override
    protected void initData() {
        super.initData();
        title.setText(R.string.application_name);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getNetInfo();
        MobclickAgent.onPageStart("线路申请");
        MobclickAgent.onResume(this); //统计时长
    }

    public void getNetInfo(){
        Observable.create(new ObservableOnSubscribe<List<ApplicationLineBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ApplicationLineBean>> e){
                try {
                    String msg = WebServicrUtils.getInstance().getMyApplicationLine(SPUtils.getUserPhone(getApplicationContext()));
                    Log.i("error",msg);
                    msg = msg.replaceAll("&","&amp;");
                    InputStream in_nocode = new ByteArrayInputStream(msg.getBytes("UTF-8"));
                    e.onNext(WebServicrUtils.getInstance().getApplcationLine(in_nocode));
                } catch (IOException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                } catch (XmlPullParserException e1) {
                    e1.printStackTrace();
                    e.onError(null);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    e.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ApplicationLineBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<ApplicationLineBean> s) {
                        if (s!=null && s.size()>0){
                            ApplicationLineBean bean = s.get(s.size()-1);
                            home.setText(bean.getHome());
                            work.setText(bean.getWork());
                            time.setText(bean.getStartTime());
                            time1.setText(bean.getEndTime());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ApplicationLineActivity.this,ApplyActivity.class));
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("线路申请");
        MobclickAgent.onPause(this);
    }
}
